<?php
class Products_model extends MY_Model {
	private $module = 'products';
	private $table = 'products';
	
	function rand_string($length) {
		$chars = "0123456789";
		$size = strlen( $chars );
		for( $i = 0; $i < $length; $i++ ) {
			$str .= $chars[ rand( 0, $size - 1 ) ];
		}
		return $str;
	}

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('contentTxt')!='' && $this->input->post('contentTxt')!='type here...'){
			$this->db->where('(`title` LIKE "%'.$this->input->post('contentTxt').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('contentTxt')!='' && $this->input->post('contentTxt')!='type here...'){
			$this->db->where('(`title` LIKE "%'.$this->input->post('contentTxt').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results($this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}
		
		if($this->input->post('newAdmincp')=='on'){
			$new = 1;
		}else{
			$new = 0;
		}
		
		if($this->input->post('sellerAdmincp')=='on'){
			$seller = 1;
		}else{
			$seller = 0;
		}
		
		if($this->input->post('promotionAdmincp')=='on'){
			$promotion = 1;
		}else{
			$promotion = 0;
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			if($this->input->post('titleAdmincp')==''){
				print 'error-title-exists';
				exit;
			}
			
			if($this->input->post('priceOldAdmincp')==''){
				print 'error-price-exists';
				exit;
			}
			
			if(empty($_FILES)){
				print 'error-image-exists';
				exit;	
			}
			
			if($this->input->post('contentAdmincp')=='' || $this->input->post('contentAdmincp')=='<br>'){
				print 'error-content-exists';
				exit;
			}
			
			$str= random_string('numeric',4);
			
			$data = array(
				'code'=> $this->input->post('folderAdmincp').$this->input->post('numberAdmincp'),
				'title'=> $this->input->post('titleAdmincp'),
				'slug'=> $this->input->post('slugAdmincp'),
				'idCategory'=> $this->input->post('categoryAdmincp'),
				'news'=> $new,
				'sellers'=> $seller,
				'promotion'=> $promotion,
				'price_new'=> $this->input->post('priceNewAdmincp'),
				'price_old'=> $this->input->post('priceOldAdmincp'),
				'detail'=> $this->input->post('contentAdmincp'),
				'image'=> $this->input->post('folderAdmincp').'/'.$fileName['image'],
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert($this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			if($this->input->post('titleAdmincp')==''){
				print 'error-title-exists';
				exit;
			}
			
			if($this->input->post('priceOldAdmincp')==''){
				print 'error-price-exists';
				exit;
			}
			
			if($this->input->post('contentAdmincp')=='' || $this->input->post('contentAdmincp')=='<br>'){
				print 'error-content-exists';
				exit;
			}
			
			$folderTmp=explode('/',$result[0]->image);
			
			//Xử lý xóa hình khi update thay đổi hình
			if($fileName['image']==''){
				$fileName['image'] = $result[0]->image;
				$data = array(
					'title'=> $this->input->post('titleAdmincp'),
					'idCategory'=> $this->input->post('categoryAdmincp'),
					'news'=> $new,
					'sellers'=> $seller,
					'promotion'=> $promotion,
					'price_new'=> $this->input->post('priceNewAdmincp'),
					'price_old'=> $this->input->post('priceOldAdmincp'),
					'detail'=> $this->input->post('contentAdmincp'),
					'image'=> $fileName['image'],
					'status'=> $status,
				);
			}else{
				@unlink(BASEFOLDER.IMAGES_PRODUCTS.$folderTmp[0].'/'.$folderTmp[1]);
				$data = array(
					'title'=> $this->input->post('titleAdmincp'),
					'idCategory'=> $this->input->post('categoryAdmincp'),
					'news'=> $new,
					'sellers'=> $seller,
					'promotion'=> $promotion,
					'price_new'=> $this->input->post('priceNewAdmincp'),
					'price_old'=> $this->input->post('priceOldAdmincp'),
					'detail'=> $this->input->post('contentAdmincp'),
					'image'=> $this->input->post('folderAdmincp').'/'.$fileName['image'],
					'status'=> $status,
				);
			}
			//End xử lý xóa hình khi update thay đổi hình
			
			
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update($this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	function checkData($title){
		$this->db->select('id');
		$this->db->where('title',$title);
		$this->db->limit(1);
		$query = $this->db->get($this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}
	
	function checkSlug($slug){
		$this->db->select('id');
		$this->db->where('slug',$slug);
		$this->db->limit(1);
		$query = $this->db->get($this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}
	
	function getFolder(){
		$this->db->select('folder');
		$query = $this->db->get('category');

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getOneFolder($id){
		$this->db->select('folder');
		$query = $this->db->get('category');

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getCategory(){
		$query = $this->db->get('category');

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getOneCategory($id){
		$this->db->where('idCategory',$id);
		$query = $this->db->get('category');

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getOtherCategory($id){
		$this->db->where('idCategory !=',$id);
		$query = $this->db->get('category');

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getOneIdProduct($id){
		$this->db->where('id',$id);
		$query= $this->db->get($this->table);
		
		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	/**************************************************************/
	/* fontend */
	/**************************************************************/
	function getTotal(){
		$this->db->select('*');
		$this->db->where('status',1);
		$query = $this->db->count_all_results($this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function getsearch($limit,$page){
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->limit($limit,$page);
		$this->db->order_by('id','DESC');
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getProNew(){
		$this->db->select('*');
		$this->db->where('news',1);
		$this->db->where('status',1);
		$this->db->limit(8);
		$this->db->order_by('id','DESC');
				
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getProSell(){
		$this->db->where('sellers',1);
		$this->db->where('status',1);
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getProPromotion(){
		$this->db->where('promotion',1);
		$this->db->where('price_new !=',0);
		$this->db->where('status',1);
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getAll(){
		$this->db->order_by('id','DESC');
		$this->db->where('status',1);
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getOne($id){
		$this->db->where('id',$id);
		$this->db->where('status',1);
		$query= $this->db->get($this->table);
		
		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotal_getProByCate($id){
		$this->db->select('*');
		$this->db->where('idCategory',$id);
		$this->db->where('status',1);
		$query = $this->db->count_all_results($this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function getsearch_getProByCate($id,$limit,$page){
		$this->db->select('*');
		$this->db->where('idCategory',$id);
		$this->db->where('status',1);
		$this->db->limit($limit,$page);
		$this->db->order_by('id','DESC');
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getsearchTotalPro($title){
		$this->db->select('*');
		$this->db->where('(`title` LIKE "%'.$title.'%")');
		$this->db->where('status',1);
		$query = $this->db->count_all_results($this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function getsearchProduct($title,$limit,$page){
		$this->db->select('*');
		$this->db->where('(`title` LIKE "%'.$title.'%")');
		$this->db->where('status',1);
		$this->db->limit($limit,$page);
		$this->db->order_by('id','DESC');
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
}